// pages/AddMember/AddMember.js
import upload from '../../utils/upload'
import {
  salesCreate,updateSales
} from "../../utils/api.js"
Page({
  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    businessId: '',//有值就是新增


    submitForm:{
      name: '',
      phone: '',
      // money99:0,
      profitRate:'',
      type:"store",
      businessId:'',
      id:''
    },
   


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options,"options")
    if (options.info) {
      let info=JSON.parse(options.info)
      console.log(info,"info")
      this.setData({
        'submitForm.id': info.id,
        'submitForm.name': info.name,
        'submitForm.phone': info.phone,
        'submitForm.profitRate': info.profitRate,
      })
    } else {
      this.setData({
        businessId: options.businessId,
        'submitForm.businessId':options.businessId
      })
    }
  },
  bindInputContent(e){
    console.log(e.currentTarget.dataset.type)
    let type=e.currentTarget.dataset.type
    if(type==1){
      this.setData({
        'submitForm.phone': e.detail.value
      })
    }else if(type==2){
      this.setData({
        'submitForm.money99': e.detail.value
      })
    }else if(type==3){
      this.setData({
        'submitForm.profitRate': e.detail.value
      })
    }else{
      this.setData({
        'submitForm.name': e.detail.value
      })
    }
    
  },
  // 头像
  uploadAvatar(){
    let that = this
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        upload.uploadMed(tempFilePaths[0]).then(img => {
          that.setData({
            avatar: img
          })
        })
      }
    })
  },
  //使用递归的方式实现数组、对象的深拷贝
  deepClone(obj) {
    let objJson=JSON.stringify(obj);
    let objClone=JSON.parse(objJson);
    return objClone;
  },
  // 新增员工
  AddMemberMed(){
    let that = this
    if (that.data.submitForm.phone && that.data.submitForm.name && that.data.submitForm.profitRate) {
        let data= this.deepClone(this.data.submitForm)
        // data.money99= Number(parseFloat(data.money99 * 100).toFixed(0))
        data.profitRate= Number(data.profitRate)
        if(this.data.businessId){
          salesCreate(data).then(res=>{
            if(res.code==200){
              wx.showToast({
                title: `新增成功`,
                icon:'none',
                success() {
                  setTimeout(function() {
                    wx.navigateBack({
                      delta: 1
                    })
                  },1000)
                }
              })
            }else{
              wx.showToast({
                title: res.message,
                icon:'none'
              })
            }
          })
        }else{
          updateSales(data).then(res=>{
            if(res.code==200){
              wx.showToast({
                title: `修改成功`,
                icon:'none',
                success() {
                  setTimeout(function() {
                    wx.navigateBack({
                      delta: 1
                    })
                  },1000)
                }
              })
            }else{
              wx.showToast({
                title: res.message,
                icon:'none'
              })
            }
          })
        }
    }else{
      wx.showToast({
        title: '请填写完所有信息',
        icon:'none'
      })
    }
  }
})